﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
// Copyright 2009, 2010 Matvei Stefarov <me@matvei.org>
using System;

namespace ForgeCraft
{
    public class CmdPinfo : Command
    {
        public override string name { get { return "pinfo"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "player"; } }
        public override bool consoleUsable { get { return false; } }
        public override Permission defaultRank { get { return Permission.Guest; } }
        public CmdPinfo() { }

        public override void Use(Player p, string message)
        {
            string name = message;
            if (name == null)
            {
                name = p.name;
            }
            else if (p.info.group.permission < Permission.Operator)
            {
                p.NoAccessMessage(Permission.Operator);
                return;
            }

            Player target = Server.FindPlayerByNick(name);
            if (target != null && target.nick != target.name)
            {
                p.Message(Color.Red + "Warning: Player named " + target.name + " is using a nickname \"" + target.nick + "\"");
                p.Message(Color.Red + "The information below is for the REAL " + name);
            }

            PlayerInfo info;
            if (!PlayerDB.FindPlayerInfo(name, out info))
            {
                p.ManyPlayersMessage(name);
            }
            else if (info != null)
            {

                if (DateTime.Now.Subtract(info.lastLoginDate).TotalDays < 1)
                {
                    p.Message(String.Format("About {0}: Last login {1:F1} hours ago from {2}",
                                                        info.name,
                                                        DateTime.Now.Subtract(info.lastLoginDate).TotalHours,
                                                        info.lastIP));
                }
                else
                {
                    p.Message(String.Format("About {0}: Last login {1:F1} days ago from {2}",
                                                        info.name,
                                                        DateTime.Now.Subtract(info.lastLoginDate).TotalDays,
                                                        info.lastIP));
                }
                p.Message(String.Format("  Logged in {0} time(s) since {1:dd MMM yyyy}.",
                                                    info.timesVisited,
                                                    info.firstLoginDate));

                p.Message(String.Format("  Built {0} and deleted {1} blocks, and wrote {2} messages.",
                                                    info.blocksBuilt,
                                                    info.blocksDeleted,
                                                    info.linesWritten));

                TimeSpan totalTime = info.totalTimeOnServer;
                if (Server.FindPlayerExact(p.name) != null)
                {
                    totalTime = totalTime.Add(DateTime.Now.Subtract(info.lastLoginDate));
                }
                p.Message(String.Format("  Spent a total of {0:F1} hours ({1:F1} minutes) here.",
                                                    totalTime.TotalHours,
                                                    totalTime.TotalMinutes));
            }
            else
            {
                p.NoPlayerMessage(name);
            }
        }
        public override void Help(Player p)
        {
            p.Message(Color.Aqua + "/pinfo " + Color.Help + "- Shows player info");           
        }
    }
}